package com.microservice.salmon.oauth.service.rbac.impl;

import com.microservice.salmon.db.entities.User;
import com.microservice.salmon.db.mapper.UserMapper;
import com.microservice.salmon.oauth.service.rbac.WebUserRbacService;
import com.salmon.common.utils.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * <p>
 * 类的描述信息
 * </p>
 *
 * @author 王洪庆 2019-01-22 09:20
 */
@Service
public class WebUserRbacServiceImpl implements WebUserRbacService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 注册用户
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public int registerUser(User user) {
        user.setId(null);
        return userMapper.insertSelective(user);
    }

    /**
     * 通过手机号、邮箱 登录名 获取用户信息
     *
     * @param loginFlag 用户标识 手机号、邮箱 登录名
     * @return
     */
    @Override
    public User obtainUser(String loginFlag) {
        Example example = new Example(User.class);
        example.selectProperties("id","loginName","password","userType","name","birthday","sex","photo",
                "qq","mobile","phone","email","lastTime","userStatus","dataFlag");
        Example.Criteria criteria = example.createCriteria();
        if(ValidateUtils.validateEamil(loginFlag)) { //是否是邮箱登录
            criteria.andEqualTo("email", loginFlag);
        } else if(ValidateUtils.validateMobile(loginFlag)) { // 是否是手机登录
            criteria.andEqualTo("mobile", loginFlag);
        } else {
            criteria.andEqualTo("loginName", loginFlag);
        }
        return userMapper.selectOneByExample(example);
    }
}
